<?php

/**
 *  推送信息表
 */
defined('InNcPHP') or exit('Access Invalid!');

class jpush_listModel extends Model{


	public function __construct(){
		parent::__construct('jpush_list');
        $this->message=0;
        $this->title=0;
        $this->alias=0;
        $this->tag=0;
        $this->registrationIds=0;
        $this->keyarray=array();
        $this->type=0;
	}






    /**
     * @param $deviceid  设置设备ID 到用户名 alias
     * @param $uid
     * @param $group_id 分组id
     * @return string|void
     */
    public function updateDevice($deviceid,$alias,$group_id){
        ini_set("display_errors", "On");
        $model = Model('config');
        $config = $model->getConfigList(array('scope'=>'appset'));
        include_once BASE_DATA_PATH.'/vendors/JPush/JPush.php';
        $client = new JPush($config['_jpush_appkey'], $config['_jpush_secret']);
        try{
            $result = $client->device()->updateDevice($deviceid, $alias,null,$group_id);
            return $result;
        }catch (Exception $e){
            return $e->getMessage();
        }
    }


    /** 添加到组里
     * @param $group_id
     * @param $array
     * @return string|void
     */
    public function updateGroupTag($group_id,$array){
        ini_set("display_errors", "On");
        $model = Model('config');
        $config = $model->getConfigList(array('scope'=>'appset'));
        require_once BASE_DATA_PATH.'/vendors/JPush/JPush.php';
        $client = new JPush($config['_jpush_appkey'], $config['_jpush_secret']);
        try{
            $result = $client->device()->updateTag("group_id".$group_id, $array);
            return $result;
        }catch (Exception $e){
            return $e->getMessage();
        }
    }


    /**
     * @param $tag
     * @param $array
     * @return string|void
     */
    public function addTag($tag,$array){
        ini_set("display_errors", "On");
        $model = Model('config');
        $config = $model->getConfigList(array('scope'=>'appset'));
        require_once BASE_DATA_PATH.'/vendors/JPush/JPush.php';
        $client = new JPush($config['_jpush_appkey'], $config['_jpush_secret']);
        try{
            $result = $client->device()->updateTag('member_id'.$tag, $array);
            return $result;
        }catch (Exception $e){
            return $e->getMessage();
        }
    }


    public function getAllTags(){
        ini_set("display_errors", "On");
        $model = Model('config');
        $config = $model->getConfigList(array('scope'=>'appset'));
        require_once BASE_DATA_PATH.'/vendors/JPush/JPush.php';
        $app_key = $config['_jpush_appkey'];
        $master_secret = $config['_jpush_secret'];
        $this->keyarray=array('username'=>$app_key,'password'=>$master_secret);
        $client = new JPush($app_key, $master_secret);
        $result = $client->device()->getTags();
        return $result;
    }

    public function getAllAlias(){
        ini_set("display_errors", "On");
        $model = Model('config');
        $config = $model->getConfigList(array('scope'=>'appset'));
        require_once BASE_DATA_PATH.'/vendors/JPush/JPush.php';
        $app_key = $config['_jpush_appkey'];
        $master_secret = $config['_jpush_secret'];
        $this->keyarray=array('username'=>$app_key,'password'=>$master_secret);
        $client = new JPush($app_key, $master_secret);
        $result = $client->device()->getTags();
        return $result;
    }


    /**
     * 发送 推送
     * @throws CException
     */
    public function Send(){
        ini_set("display_errors", "On");
        $model = Model('config');
        $config = $model->getConfigList(array('scope'=>'appset'));
        require_once BASE_DATA_PATH.'/vendors/JPush/JPush.php';

        $app_key = $config['_jpush_appkey'];
        $master_secret = $config['_jpush_secret'];
        $client = new JPush($app_key, $master_secret);
        try{
            if($this->alias){ //发送别名
                //发消息类型 text - 文本，image - 图片, custom - 自定义消息
                $result = $client->push()
                    ->setPlatform("all")
                    ->addAlias(($this->alias))
                    ->setNotificationAlert($this->message)
                    ->setMessage($this->message, $this->title, 'custom', $this->keyarray)
                    ->setOptions(null, 86400*7, null, false)
                    ->send();
            }elseif($this->tag){ //发送分组
                $result = $client->push()
                    ->setPlatform("all")
                    ->addTag(($this->tag))
                    ->setNotificationAlert($this->message)
                    ->setMessage($this->message, $this->title, 'custom', $this->keyarray)
                    ->setOptions(null, 86400*7, null, false)
                    ->send();
            }elseif($this->registrationIds){ //发送设备号
                $result = $client->push()
                    ->setPlatform("all")
                    ->addRegistrationId(($this->registrationIds))
                    ->setNotificationAlert($this->message)
                    ->setMessage($this->message, $this->title, 'custom', $this->keyarray)
                    ->setOptions(null, 86400*7, null, false)
                    ->send();

            }else{  //群发
                $result = $client->push()
                    ->setPlatform("all")
                    ->addAllAudience()
                    ->setNotificationAlert($this->message)
                    ->setMessage($this->message, $this->title, 'type', $this->keyarray)
                    ->setOptions(null, 86400*7, null, false)
                    ->send();
            }
            return $result;
        }catch (Exception $e){
            return false;
            exit;
        }
        return $result;
    }



    // 添加
    public function add($param){
        return $this->insert($param);
    }

    // 更新
    public function update($condition, $param){
        return $this->where($condition)->update($param);
    }

    // 删除
    public function del($condition){
        return $this->where($condition)->delete();
    }

}
?>